/*
 * Copyright (c) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
/*
 * This file was generated.
 *  with google-apis-code-generator 1.1.0 (build: 2011-09-09 16:53:41 CDT)
 */

package com.google.api.services.gan;

import com.google.api.client.googleapis.GoogleHeaders;
import com.google.api.client.googleapis.GoogleUrl;
import com.google.api.client.googleapis.GoogleHeaders;
import com.google.api.client.googleapis.GoogleUrl;
import com.google.api.client.googleapis.MethodOverride;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
import com.google.api.client.http.HttpMethod;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.json.JsonHttpContent;
import com.google.api.client.http.json.JsonHttpParser;
import com.google.api.client.json.JsonFactory;

import java.io.IOException;
import java.util.HashMap;

/**
 * @author aiuto@google.com (Tony Aiuto)
 */
class ApiClient {

  /**
   * Returns response with indentations and line breaks.  [default: true]
   */
  private Boolean prettyPrint;

  /** Returns response with indentations and line breaks. */
  public Boolean getPrettyPrint() {
    return prettyPrint;
  }

  /** Returns response with indentations and line breaks. */
  public void setPrettyPrint(Boolean prettyPrint) {
    this.prettyPrint = prettyPrint;
  }

  /**
   * Selector specifying which fields to include in a partial response.
   */
  private String fields;

  /** Selector specifying which fields to include in a partial response. */
  public String getFields() {
    return fields;
  }

  /** Selector specifying which fields to include in a partial response. */
  public void setFields(String fields) {
    this.fields = fields;
  }

  /**
   * OAuth 2.0 token for the current user.
   */
  private String oauthToken;

  /** OAuth 2.0 token for the current user. */
  public String getOauthToken() {
    return oauthToken;
  }

  /** OAuth 2.0 token for the current user. */
  public void setOauthToken(String oauthToken) {
    this.oauthToken = oauthToken;
  }

  /**
   * API key. Your API key identifies your project and provides you with API access, quota, and
   * reports. Required unless you provide an OAuth 2.0 token.
   */
  private String key;

  /**
 * API key. Your API key identifies your project and provides you with API access, quota, and
 * reports. Required unless you provide an OAuth 2.0 token.
 */
  public String getKey() {
    return key;
  }

  /**
 * API key. Your API key identifies your project and provides you with API access, quota, and
 * reports. Required unless you provide an OAuth 2.0 token.
 */
  public void setKey(String key) {
    this.key = key;
  }

  /**
   * IP address of the site where the request originates. Use this if you want to enforce per-user
   * limits.
   */
  private String userIp;

  /**
 * IP address of the site where the request originates. Use this if you want to enforce per-user
 * limits.
 */
  public String getUserIp() {
    return userIp;
  }

  /**
 * IP address of the site where the request originates. Use this if you want to enforce per-user
 * limits.
 */
  public void setUserIp(String userIp) {
    this.userIp = userIp;
  }

  /**
   * Data format for the response.  [default: json]
   */
  private String alt;

  /** Data format for the response. */
  public String getAlt() {
    return alt;
  }

  /** Data format for the response. */
  public void setAlt(String alt) {
    this.alt = alt;
  }



  /**
   * Sets the Google APIs key. This identifies your application for analytics and
   * quota purposes. To create an apiKey for your application, open the
   * <b>API Access</b> tab in the
   * <a href="https://code.google.com/apis/console#access">
   * Google APIs developer console</a> and see the "Simple API Access" section.
   * @deprecated (scheduled to be removed in the near future) Use {@link #setKey}
   */
  @Deprecated
  public void setApiKey(String apiKey) {
    setKey(apiKey);
  }

  /**
   * Returns the API key, or {@code null} if none was set.
   * @deprecated (scheduled to be removed in the near future) Use {@link #getKey}
   */
  @Deprecated
  public String getApiKey() {
    return getKey();
  }


  /**
   * The request factory for connections to the server.
   */
  final HttpRequestFactory requestFactory;

  /**
   * The JSON factory to use for parsing and serializing requests.
   */
  final JsonFactory jsonFactory;

  private String baseServer;

  /**
   * Sets the URL of the server hosting the API, for example {@code "https://www.googleapis.com"}.
   * This is generally set in the subclass constructor, but may be changed to use an alternate
   * server, typically during testing of a new service.
   */
  public void setBaseServer(String baseServer) {
    this.baseServer = baseServer;
  }

  /**
   * Returns the base server URL.
   */
  public String getBaseServer() {
    return baseServer;
  }

  private String basePath;

  /**
   * Sets the base path of the service on the server, for example {@code "/gan/v1beta1"}. Must be
   * URL-encoded. This is determined when the library is generated and normally should not be
   * changed.
   */
  public void setBasePath(String basePath) {
    this.basePath = basePath;
  }

  /**
   * Returns the base path of the service on the server, for example {@code "/gan/v1beta1"}.
   */
  public String getBasePath() {
    return basePath;
  }

  /**
   * The JSON parser to user for parsing requests.
   */
  final JsonHttpParser jsonParser;

  /**
   * The application name to be sent in the User-Agent header of each request.
   */
  private String userAgent;

  /**
   * The access token to be sent in the header of the request.
   */
  private String accessToken;

  /**
   * {@link MethodOverride} to ensure that methods get overridden correctly.
   */
  private final MethodOverride methodOverride = new MethodOverride();

  /**
   * Construct the {@link ApiClient }
   *
   * @param baseServer The base part of a URL indicating the server to send request to.
   * @param basePath The common base of the the path for all service requests.
   * @param transport The transport to use for requests.
   * @param requestInitializer The initializer to use when creating an {@link HttpRequest}.
   * @param jsonFactory A factory for creating JSON parsers and serializers.
   */
  protected ApiClient(
      String baseServer, String basePath,
      HttpTransport transport,
      HttpRequestInitializer requestInitializer,
      JsonFactory jsonFactory) {
    this.baseServer = baseServer;
    this.basePath = basePath;
    this.jsonFactory = jsonFactory;
    
    this.jsonParser = new JsonHttpParser(this.jsonFactory);
    
    this.requestFactory = transport.createRequestFactory(requestInitializer);
  }

  /**
   * Applies an OAuth 2.0 access token to all requests from this client.
   *
   * @deprecated (scheduled to be removed in the near future) Use
   *             {@code com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource}
   *             or {@code com.google.api.client.googleapis.extensions.auth.helpers.oauth2.draft10.GoogleOAuth2ThreeLeggedFlow}
   */
  @Deprecated
  public void setAccessToken(String accessToken) {
    this.accessToken = accessToken;
  }

  /**
   * Sets the application name to be used in the UserAgent header of each request.
   *
   * @param name The application name. This string will be embedded in the User-Agent header of
   * subsequent services requests.
   */
  public void setApplicationName(String name) {
    userAgent = name;
  }

  /**
   * Create an {@link HttpRequest} suitable for use against this service.
   *
   * @param method {@link HttpMethod} type
   * @return newly created {@link HttpRequest}
   */
  HttpRequest buildHttpRequest(HttpMethod method,
                               String requestUrl,
                               Gan.RemoteRequest request)
       throws IOException {
    GoogleUrl url = GoogleUrl.create(baseServer + basePath, requestUrl, request);
    url.key = key;
    url.alt = "json";
    url.prettyprint = prettyPrint;
    url.userip = userIp;

    HttpRequest httpRequest = requestFactory.buildRequest(method, url, null);
    if (userAgent != null) {
      httpRequest.getHeaders().setUserAgent(userAgent);
    }
    methodOverride.intercept(httpRequest);
    if (accessToken != null) {
      new GoogleAccessProtectedResource(accessToken).intercept(httpRequest);
    }
    return httpRequest;
  }

  /**
   * Create a JSON serializer for a request object.
   *
   * @param body A POJO that can be serialized into JSON.
   */
  JsonHttpContent createSerializer(Object body) {
    return new JsonHttpContent(jsonFactory, body);
  }
}
